package com.yuanfeng.job.service.impl;

import com.yuanfeng.commoms.dto.order.RefundParamDTO;
import com.yuanfeng.commoms.dto.order.WapGoodsSaleSumDTO;
import com.yuanfeng.commoms.util.DateUtils;
import com.yuanfeng.job.dto.AppJiPushOrderDTO;
import com.yuanfeng.job.dto.BusinessOvertimeOrderDTO;
import com.yuanfeng.job.dto.OrderStatusDTO;
import com.yuanfeng.job.dto.RefundParamTwoDTO;
import com.yuanfeng.job.feign.PayMentServiceClient;
import com.yuanfeng.job.mapper.UpdateOrderStatusMapper;
import com.yuanfeng.job.service.UpdateOrderStatusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Stranger
 * @date 2023/5/17 14:32
 */
@Service
public class UpdateOrderStatusServiceImpl implements UpdateOrderStatusService {
    @Resource
    private UpdateOrderStatusMapper updateOrderStatusMapper;
    @Autowired
    private PayMentServiceClient paymentServiceClient;

    private static final Logger logger = LoggerFactory.getLogger(UpdateOrderStatusService.class);
    @Override
    public List<AppJiPushOrderDTO> listForUpdateOrder() {
        return updateOrderStatusMapper.listForUpdateOrder();
    }

    @Override
    public Integer updateForUpdateOrder(String orderId) {
        return updateOrderStatusMapper.updateForUpdateOrder(orderId);
    }

    @Override
    public Integer updateForUpdateGoods(String orderId) {
        return updateOrderStatusMapper.updateForUpdateGoods(orderId);
    }

    @Override
    public List<String> listTimeoutOrder2(String minutes) {
        return updateOrderStatusMapper.listTimeoutOrder2(minutes);
    }

    @Override
    public List<WapGoodsSaleSumDTO> queryGoodsXinXi(String orderId) {
        return updateOrderStatusMapper.queryGoodsXinXi(orderId);
    }

    @Override
    public Map<String, Long> queryGoodsCommonStock(Integer goodsId) {
        return updateOrderStatusMapper.queryGoodsCommonStock(goodsId);
    }

    @Override
    public Integer updateCommonStockByGoodsId(WapGoodsSaleSumDTO param) {
        return updateOrderStatusMapper.updateCommonStockByGoodsId(param);
    }

    @Override
    public Integer updateGoodsStockByGoodsId(WapGoodsSaleSumDTO param) {
        return updateOrderStatusMapper.updateGoodsStockByGoodsId(param);
    }

    @Override
    public List<OrderStatusDTO> listTimeoutOrderDetail2(String minutes) {
        return updateOrderStatusMapper.listTimeoutOrderDetail2(minutes);
    }

    @Override
    public List<Integer> queryOrderStatus(String paymentNumber) {
        return updateOrderStatusMapper.queryOrderStatus(paymentNumber);
    }

    @Override
    public Integer updateTimeoutOrder2(String minutes) {
        return updateOrderStatusMapper.updateTimeoutOrder2(minutes);
    }

    @Override
    public Integer updateTimeoutOrderGoods(String orderId) {
        return updateOrderStatusMapper.updateTimeoutOrderGoods(orderId);
    }

    @Override
    public Integer editApprovalId(String orderId) {
        return updateOrderStatusMapper.editApprovalId(orderId);
    }

    @Override
    public void refundOrderHandleJob() {
        // 查询超过48小时商家未处理的订单
        List<BusinessOvertimeOrderDTO> pojoList = updateOrderStatusMapper.queryReturnOrderNotHandle();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String finishTime = dateformat.format(new Date());
        if (pojoList.size() >= 1) {
            for (BusinessOvertimeOrderDTO pojo : pojoList) {
                pojo.setReturnShopHandle(5);
                pojo.setReturnState(5);
                pojo.setReturnShopMessage("超时未处理，自动同意");
                pojo.setReturnShopTime(finishTime);
                pojo.setReturnFinishTime(finishTime);
                pojo.setOrderStatus(6);
                pojo.setOrderRefundStatus(2);
                pojo.setOrderFinishedTime(finishTime);

                String uOrderId = updateOrderStatusMapper.queryPaymentNumber(pojo);
                List<Integer> orderTradeStatus = updateOrderStatusMapper.queryOrderRecordStatus(uOrderId);

                RefundParamDTO refundPara = new RefundParamDTO();
                boolean isRefundSuccess = false;
                String orderId = pojo.getOrderId();
                RefundParamTwoDTO refundParaTwo = updateOrderStatusMapper.queryRefundPara(orderId);
                refundPara.setOrderFrom(refundParaTwo.getOrderFrom());
                refundPara.setOrderId(refundParaTwo.getOrderId());
                refundPara.setOrderTradeStatus(orderTradeStatus);
                refundPara.setPayCode(refundParaTwo.getPayCode());
                refundPara.setReturnCode(refundParaTwo.getReturnCode());
                refundPara.setPaymentOtherNumber(refundParaTwo.getPaymentOtherNumber());
                isRefundSuccess = paymentServiceClient.waitShipmentsRefund(refundPara);
                if (isRefundSuccess == false) {
                    logger.info("====退款false 跳过====");
                    continue;
                }else{
                    //更新订单表状态
                    Integer torf = updateOrderStatusMapper.updateBusinessOvertimeOrderBase(pojo);
                    //更新退款
                    updateOrderStatusMapper.updateBusinessOvertimeOrderReturn(pojo);
                }
            }
        }

        logger.info("更新超过48小时商家未处理售后退款订单  更新时间" + DateUtils.getNowDate());
    }
}
